package com.jshop.search.controller;

import com.jshop.search.model.ProductEntity;
import com.jshop.search.service.ProductService;
import com.jshop.seckill.http.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;


@Slf4j
@RequestMapping(value = "/search/save")
@RestController
public class ElasticSaveController {

    @Autowired
    private ProductService productSaveService;


    /**
     * 上架商品
     * @param productEntities
     * @return
     */
    @PostMapping(value = "/product")
    public HttpResult productStatusUp(@RequestBody List<ProductEntity> productEntities) {

        boolean status=false;
        try {
            status = productSaveService.productStatusUp(productEntities);
        } catch (IOException e) {
            log.error("商品上架异常",e);
            HttpResult.error(500, e.getMessage());
        }

        if(status){
            HttpResult.error(400, "部分商品上架失败");
        }else {
            return HttpResult.ok();
        }
        return HttpResult.ok();
    }


}
